package com.ailink.basic.string;

/**
 * Created by Administrator on 2018/3/3
 */
public class MyTestString {

    public static final int COUNT = 10000000;

    private static void test1() {
        String str = "";
        long beginTime=System.currentTimeMillis();
        for(int i=0;i<COUNT;i++){
            str = "Hello" + "world";
        }
        long endTime=System.currentTimeMillis();
        System.out.println(endTime-beginTime);
    }

    private static void test2() {
        String s1 = "hello";
        String s2 = "world";
        String str = "";
        long beginTime=System.currentTimeMillis();
        for(int i=0;i<COUNT;i++){
            str = s1 + s2;
        }
        long endTime=System.currentTimeMillis();
        System.out.println(endTime-beginTime);
    }

    private static void test3() {
        String s1 = "hello";
        String str = "";
        long beginTime=System.currentTimeMillis();
        for(int i=0;i<10000;i++){
            str = str + s1;
        }
        long endTime=System.currentTimeMillis();
        System.out.println(endTime-beginTime);
    }

    private static void test4() {
        String s1 = "hello";
        StringBuilder str = new StringBuilder();
        long beginTime=System.currentTimeMillis();
        for(int i=0;i<COUNT;i++){
            str = str.append(s1);
        }
        long endTime=System.currentTimeMillis();
        System.out.println(endTime-beginTime);
    }

    private static void test5() {
        String s1 = "hello";
        StringBuffer str = new StringBuffer();
        long beginTime=System.currentTimeMillis();
        for(int i=0;i<COUNT;i++){
            str = str.append(s1);
        }
        long endTime=System.currentTimeMillis();
        System.out.println(endTime-beginTime);
    }

    public static void main(String[] args) {
//        test1();
//        test2();
//        test3();
//        test4();
//        test5();

        String s1 = "Hello";
        String s2 = s1;
        System.out.println(s1);
        System.out.println(Integer.toHexString(System.identityHashCode(s1)));
        System.out.println(Integer.toHexString(System.identityHashCode(s2)));
        s1 = s1 + " World!";
        System.out.println(s1);
        System.out.println(Integer.toHexString(System.identityHashCode(s1)));
        System.out.println(Integer.toHexString(System.identityHashCode(s2)));

        String a1 = "abc";
        String a2 = new String("abc");
        System.out.println(Integer.toHexString(System.identityHashCode(a1)));
        System.out.println(Integer.toHexString(System.identityHashCode(a2)));
        a2 = a2.intern();
        System.out.println(Integer.toHexString(System.identityHashCode(a1)));
        System.out.println(Integer.toHexString(System.identityHashCode(a2)));

        String c1 = "ja";
        String c2 = "va";
        String c3 = c1 + c2;
        Object o1 = new Object();
//        o1.equals()
//        System.out.println(c3);
    }
}
